#include "doubleList.h"

doubleList::doubleList(){
	this->dList = new vector<double>();
}

doubleList::~doubleList(){
	delete this->dList;
}

// Grigoropoulos Panagiwtis
//Xrisimopoiw to #include <time.h> kai to #include <assert.h>
//H routina 1 einai i sunartisi double * vectortoarray(); , kanei return to array pou einai copy tou vector
double * doubleList::vectortoarray()
{
	int i;
	double *arrayout = new double[dList->size()];

	for(i=0;i<dList->size();i++)
		arrayout[i]=dList->at(i);


	return arrayout;
}

// Antonopoulos Spyridon
// 2. bool insertTo(int pos, double num) 
// Eisagei ton arithmo num sti thesi pos tis listas
// pos :: [0,1..]
// Prepei to pos na einai thetiko i 0 kai mikrotero i iso me to megethos tis listas
// e.g insertTo(0,3.3);

bool doubleList::insertTo(int pos,double num){

	if ( pos < 0 ) {
		cerr<<"Cannot insert element "<<num<<" in position "<<pos<<" ,Negative Position Given "<<endl; 
		return false;
	}

	if ( pos > this->dList->size()) {
		cerr<<"Cannot insert element "<<num<<" in position "<<pos<<" ,Out of List Bounds"<<endl; 
		return false;
	}

	vector<double>::iterator it1 = this->dList->begin();
	// moving the iterator to position [pos]
	for (int i = 0; i < pos; i++) 
		it1++;

	int oldL = this->dList->size();
	this->dList->insert(it1,num);
	assert(this->dList->size() == oldL+1);
	return true;

}

// Sakellari Elisavet
// 3. void add_after(int i, double numb)
// prosthetei ton arithmo numb se ola ta stoixeia me to i position
// e.g add_after(0,2) [1,2,3,4] -> [2,4,5,6]
//	   add_after(1,2) [1,2,3,4] -> [1,4,5,6] ..

void doubleList::add_after(int i, double numb){
     
     if(i<0) cerr<<"Negative position number : "<< i <<endl;
     
     else if (i>= dList->size()) cerr<<"Position number bigger than the size of the list : "<< i <<endl;
     
     else {
		 for ( int j = i ; j < dList->size() ; j++ ) {
			dList->at(j)= dList->at(j) + numb;
		 }
	 }
}


// Antoniadis Anastasios
// 4. substractList
// epistrefei ena int -1 an sunevei error
// i to plitos twn 0 pou diagrafikan

int doubleList::substractList( int position, double value ) {
        vector <double> *tempVector;
        tempVector = new vector<double>;
        char buffer[64];
        int expectedLength,del;

        if ( position >= dList->size() ) {
                cerr << "Position exceeds list size" << endl;
                return -1;
        }
        if ( position < 0 ) {
                cerr << "Negative positions not accepted" << endl;
                return -1;
        }

        expectedLength = dList->size();
		del = 0;
		// position starting from 0 ..
		// e.g substractList(0,1) to a list 1,2,3,4 means -> sub 1 from all elements 0,1,2,3
		// e.g substractlist(1,1) to a list 1,2,3,4 means -> sub 1 from all elements after '1'
		// -> 1,1,2,3
        for ( int i = position ; i < dList->size() ; i++ ) {
                (*dList)[i]  = (*dList)[i] - value;      
        }
		for ( int i = 0; i < position; i++) {
				tempVector->push_back(  (*dList)[i] );	
		}

        for ( int i = position ; i < dList->size() ; i++ ) {
                char is_zero = 1;
                sprintf( buffer, "%f", (*dList)[i] );
                for ( int j = 0 ; j < strlen( buffer ) ; j++ ) 
                        if ( buffer[j] != '0' && buffer[j] != '-' && buffer[j] != '.' ) {
                                is_zero = 0;

                }
                if ( !is_zero ) 
                        tempVector->push_back(  (*dList)[i] );
                else {
						del++;
                        expectedLength--;
				}
        }
        
        assert( tempVector->size() == expectedLength );
        delete this->dList;
        dList = tempVector;

        return del;
}


// Sakellari Elisavet
// 5. insert_rand(double numb) eisagei ton arithmo numd se mia tuxaia thesi tis listas


int doubleList::insert_rand(double numb){
     
     int random;
     
     vector<double>::iterator it,it2;
    
     srand ( time(NULL) );
     random = rand() % (dList->size()-1) + 0;
     
      it = dList->begin();
      
      vector<double> anothervector (1,numb);

      dList->insert (it+random,anothervector.begin(),anothervector.end());

      return random;
          
}

// Antonopoulos Spyridon
// 6. bool remove_ifNumInPos(int pos, double num) 
// Psaxnei an sti thesi 'pos' tis listas yparxei o arithmos 'num'
// An to brei tote ton diagrafei apo ti lista kai metakinei ta upoloipa stoixeia
// pos :: [0,1..]
// e.g remove_ifNumInPos(0,3.3)
// returns true if element 3.3 is found in pos 0 and deleted
// return false if an error happened or element not found
// Prepei to pos na einai >= 0 kai < apo to size tis listas

bool doubleList::remove_ifNumInPos(int pos, double num) {

	if ( pos < 0 ) {
		cerr<<"Cannot find element "<<num<<" in position "<<pos<<" ,Negative Position Given "<<endl; 
		return false;
	}

	if ( pos >= this->dList->size()) {
		cerr<<"Cannot find element "<<num<<" in position "<<pos<<" ,Out of List Bounds"<<endl; 
		return false;
	}

	vector<double>::iterator it1 = this->dList->begin();
	// moving the iterator to position [pos]
	for (int i = 0; i < pos; i++) 
		it1++;

	if ((*it1) == num) {
		int oldS = this->dList->size();
		this->dList->erase(it1);
		assert(this->dList->size() == oldS-1);
		return true;
	}
	else
		return false;

}

// Grigoropoulos Panagiwtis
//H routina 7 einai i sunartisi double * erasenum(); , kanei return to noumero kai tin 8esi tou
// pos :[0..]
double * doubleList::erasenum()
{
	int pos, oldL;
	double *out = new double[2];
	srand(time(NULL));

	oldL=dList->size();

	pos=(rand()%dList->size());
	assert(pos < dList->size());
	assert(pos >= 0);

	out[0]=dList->at(pos);
	out[1]=pos;			//position starting from 0

	dList->erase(dList->begin()+pos);
	assert(this->dList->size() == oldL-1);


	return out;
}


// Antoniadis Anastastios
// Routina 8

void doubleList::reverseList() {
        vector <double> *tempVector;
        tempVector = new vector<double>;
        
        for( int i = dList->size() - 1 ; i >= 0 ; i--  ) {
                tempVector->push_back( dList->at(i) );
        }
        assert( dList->size() == tempVector->size() );
        delete this->dList;
        dList = tempVector;
        
        return;
}




